person = {
    "name": "jackie",
    "age": 10,
    "city": "苏州",
    "favirote": ["football", "music", "movie"],
}

for key in person:
    print("key=", key, "value=", person[key])


# 得到字典的所有键值对元组列表 [("name","jackie"),("age",10),("ciity","苏州"),("favirote",["football", "music", "movie"])]
items = person.items()
for key, value in person.items():
    print("key=", key, "value=", value)

# 得到字典的所有key
keys = person.keys()
print("keys=", keys)

# 得到字典所有的值
values = person.values()
print("value=", value)

# 字典复制,新字典和老字典的内存地址不一样
new_person_dic = person.copy()

print("new_person_dic.id=", id(new_person_dic), "person.id=", id(person), "\n")
print(person is new_person_dic)


# 从序列创建键，所有键的值相同
new_dict = dict.fromkeys(["a", "b", "c"], 0)
print(new_dict)  # {'a': 0, 'b': 0, 'c': 0}

my_dict = {"name": "susan"}

# keyError
try:
    print(my_dict["age"])
except KeyError as e:
    print("错误为:", f"{e}不存在")

stu_age = my_dict.get("age", 30)  # 查找age的值，不存在则返回默认值30返回

print("stu_age", stu_age)

my_dict.setdefault("city", "苏州")  # 不存在则设置默认值

print("my_dict['city']", my_dict["city"])
